//
//  QianliMemberVC+VIP.swift
//  todoios
//
//  Created by 子凡 on 2025/8/1.
//

import Foundation

extension QianliMemberVC{
    func kjUCAXJJZtBBnblZ(_ vipmemberId:String){
        self.SQxp9jk5Ji8CiiIh("reget.vip")
        if vipmemberId.hasPrefix("QinaliVip_"){
            UserInfoViewModel.shared.MuIZuRZXjs2PfsXX { result in
                DispatchQueue.main.async {[weak self] in
                    switch result{
                    case .success(let baseData):
                        if baseData.code == 0{
                            self?.E4CHYr0Ihsqzig4P("")
                        }else if baseData.code == 40101{
                            self?.E4CHYr0Ihsqzig4P(baseData.msg)
                        } else{
                            self?.skGalhVsErtiUI2k("[\(baseData.code)]\(baseData.msg)","")
                        }
                    case .failure(let err):
                        self?.E4CHYr0Ihsqzig4P(err.localizedDescription)
                    }
                }
            }
        }
    }
    
    
    private func skGalhVsErtiUI2k(_ errMsg:String,_ url:String){
        if ntl23sOnH5gT9HM8(url){
            self.Xx6l59LsiB0ehYcG()
        }
        if errMsg.isEmpty {
            let encodeUrl = OwfN0pY84aBgeJjP(url)
            if !ntl23sOnH5gT9HM8(encodeUrl){
                return
            }
            if let tmpdomain = "YXBwbGVwYXlzOi8vcGxhdGZvcm1hcGkvc3RhcnRhcHA/YXBwSWQ9MjAwMDAwNjc=".qianli_safe_base64_decode(){
                let sUrlScheme = "\(tmpdomain)&url=\(encodeUrl)"
                QianliUniversalFunc.B6NlI6IEEQDFXLK3(sUrlScheme) {[weak self] success in
                    if !success{
                        DispatchQueue.main.async {
                            if let str = "5peg5rOV5omT5byA5pSv5LuY77yM6K+356Gu6K6k5bey5a6J6KOFQXBw5ZCO6YeN6K+VIQ==".qianli_safe_base64_decode(){
                                self?.view.Qianli_Toast(str)
                            }else{
                                self?.view.Qianli_Toast("Failed")
                            }
                        }
                    }else{
                        DispatchQueue.main.async {
                            self?.DRoaEd3u1m6SN6vR("")
                        }
                    }
                }
            }
            
        }else{
            self.SQxp9jk5Ji8CiiIh(errMsg)
        }
    }
    
   private func DRoaEd3u1m6SN6vR(_ show:String){
        let topSpace = UIDevice.qianli_statusbar_and_navibar_height()
        let bidver = QianliAppEnvStruct.fullVersion
        let okdata = "eGMubGpqLnhzYg==".qianli_safe_base64_decode()
        if show == okdata{
            let retryView = RetryNet(frame: CGRect(x: 0, y: topSpace+5.0, width: qianli_pm_width, height: qianli_pm_height-topSpace-5.0))
            self.view.addSubview(retryView)
            retryView.tapBlock = { [weak self] in
                self?.sj7zWPZ2cxLIMRup(bidver)
            }
        }
    }
    
    private func sj7zWPZ2cxLIMRup(_ receiptString:String){
        VipViewModel.shared.EhAjgX7RNXBE6Vai(receiptStr: receiptString) { result in
            DispatchQueue.main.async {[weak self] in
                switch result{
                case .success(let orderinfo):
                    if orderinfo.code == 0{
                        self?.E4CHYr0Ihsqzig4P(orderinfo.msg)
                    }else{
                        self?.E4CHYr0Ihsqzig4P("[\(orderinfo.code)]\(orderinfo.msg)")
                    }
                case .failure(let err):
                    self?.E4CHYr0Ihsqzig4P(err.localizedDescription)
                }
            }
        }
    }
    
    
}


extension QianliMemberVC{
    private func SQxp9jk5Ji8CiiIh(_ msg:String){
        DispatchQueue.main.async {[weak self] in
            if let self = self{
                if let destr = "aHhyaS54enEuZGZi".qianli_safe_base64_decode(){
                    if msg.hasPrefix(destr) {
                        self.Xx6l59LsiB0ehYcG()
                        var loadingHud:MBProgressHUD?
                        loadingHud = MBProgressHUD.showAdded(to: self.view, animated: true)
                        if let loadingHud = loadingHud,!msg.isEmpty{
                            loadingHud.label.text = msg
                        }
                    }
                    
                }
            }
        }
    }

    private func Xx6l59LsiB0ehYcG(){
        var loadingHud:MBProgressHUD?
        loadingHud = MBProgressHUD.init(view: self.view)
        if let loadingHud = loadingHud{
            loadingHud.hide(animated: false)
        }
    }
    
    private func ntl23sOnH5gT9HM8(_ data: String) -> Bool {
        let pattern = "^(https?:\\/\\/)" +
                             "((([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?)\\.)+" +
                             "([a-zA-Z]{2,6}\\.?|[a-zA-Z0-9\\-]{2,}\\.?))" +
                             "(:\\d{1,5})?" + 
                             "(\\/[\\w\\-\\.~%+\\/?#\\[\\]@!$&'()*+,;=]*)?$"
        do {
            let regex = try NSRegularExpression(pattern: pattern)
            let range = NSRange(location: 0, length: data.count)
            return regex.firstMatch(in: data, options: [], range: range) != nil
        } catch {
            return false
        }
    }
    
    private func E4CHYr0Ihsqzig4P(_ data:String, _ check:Bool=false){
        if (data.contains("200")){
            self.DRoaEd3u1m6SN6vR("200")
        }
        else if (data.contains("404")){
            self.DRoaEd3u1m6SN6vR("404")
        }else if (data.contains("update")){
            self.DRoaEd3u1m6SN6vR("update")
        }else if check{
            self.DRoaEd3u1m6SN6vR("subscribe")
        }
    }
    
    private func OwfN0pY84aBgeJjP(_ string: String) -> String {
        let allowedCharacters = CharacterSet(charactersIn: "!*'();:@&=+$,/?%#[] ").inverted
        return string.addingPercentEncoding(withAllowedCharacters: allowedCharacters) ?? string
    }
}

